import time
from script.testing.nowcoder import test_nc_id_is_ok
import pymongo
import csv
from model.testing.config.set import time_sleep_is_ok

def base(dateset , uid_name , form_name , test_id_is_ok):
    client =  pymongo.MongoClient("mongodb://localhost:27017/")
    db = client["ACM_MEMBER"]
    collections = db[dateset]

    with open("../../data/testing_result/" + form_name + "_testing_result.csv", "w", encoding="utf-8" , newline='') as f:
        writer = csv.writer(f)
        writer.writerow([ "student_id", "name" , uid_name , "is_ok"])
        for collection in collections.find():
            uid = collection[uid_name]
            student_id = collection["student_id"]
            name = collection["name"]
            time.sleep(time_sleep_is_ok)
            if test_id_is_ok(uid):
                writer.writerow([student_id, name, uid, " "])
            else:
                writer.writerow([student_id, name, uid, "当前" + form_name + "账户不存在,请检查"])

    client.close()

if __name__ == '__main__':
    dateset = "NOWCODER_INFORMATION"
    uid_name = "nowcoder_id"
    form_name = "nowcoder"
    base(dateset,uid_name,form_name , test_nc_id_is_ok)